/*************************************************************************
	> File Name: read_command.c
	> Author: KyleShaw
	> Mail: xtk621@gmail.com 
	> Created Time: 2014-11-27 22:35:49
 ************************************************************************/

#include <stdio.h>
#include <apue.h>
#include <sys/wait.h>
#include <sys/types.h>

int main(void)
{
	char buf[MAXLINE];
	pid_t pid;
	int status;

	printf("%% ");
	while(fgets(buf, MAXLINE, stdin) != NULL) {
		if(buf[strlen(buf) - 1] == '\n') {
			buf[strlen(buf) -1] = 0;
		}

		if((pid = fork()) < 0) {
			err_sys("fork error");
		} else if (pid == 0) {
			execlp(buf, buf, (char *)0);
			err_ret("couldn't execute %s", buf);
			exit(127);
		} 
		if((pid = waitpid(pid, &status, 0)) < 0) {
			err_sys("waitpid error");
		}
		printf("%% ");
	}

	exit(0);
}
